

use "../Lens_cleaned_V6_Domestic_V1.dta",clear
cd ../OUTPUT
di "**********************************"
di "***TABLE 2 SUM STATS*by uni corp***"
di "****ADDING MORE CONTROLS*****"

	gen unifirm=0 if uni==1
	replace unifirm=1 if firm==1

	eststo uni: quietly estpost summarize ///
		TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep $x NumInventors ClaimCounts uspc136 uspc320 uspc977  if unifirm == 0
	eststo firm: quietly estpost summarize ///
		TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep $x NumInventors ClaimCounts uspc136 uspc320 uspc977  if unifirm == 1
	eststo Difference:  estpost ttest ///
		 TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep $x NumInventors ClaimCounts uspc136 uspc320 uspc977 , by(unifirm) unequal	
	
	
esttab uni firm Difference using "${OUTPUT}sum uni firm diff_RR_V1.csv", ///
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2)) t(pattern(0 0 1) par fmt(2))") ///
label
di "**********************************"
di "***TABLE A1 all business entities and all linguistic measures"
di "****4 CATEGORIES***"
di "**********************************"
cd "../"
eststo Firms: quietly estpost summarize ///
     $x NumInventors ClaimCounts uspc136 uspc320 uspc977 $Read if firm ==1
eststo Uni: quietly estpost summarize ///
     $x NumInventors ClaimCounts uspc136 uspc320 uspc977 $Read if uni == 1
eststo Both: quietly estpost summarize ///
     $x NumInventors ClaimCounts uspc136 uspc320 uspc977 $Read if both == 1
eststo Others: quietly estpost summarize ///
     $x NumInventors ClaimCounts uspc136 uspc320 uspc977 $Read if other == 1

	esttab Firms Uni Both Others using "sum4cat.csv", ///
	cells("mean(pattern(1 1 1) fmt(2)) sd(pattern(1 1 1))") ///
	label replace
	
	
di "**********************************"	
di "***TABLE 3 BASELINE estimation******"
di "ADDING YEAR FE and NUM INVENTORS and CLAIM COUNT"
di "**********************************"

use "../Lens_cleaned_V6_Domestic_V1.dta",clear

	local name baseline_wNumInventorYearFECalimCounts_V2/*use resolved NPL*/
	local replace replace
foreach y in TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep {
		xi:reg `y' firm both other $sub ,cluster(uspc)
		outreg2 using "`name'.xls", `replace' label keep(firm both other) addtext(Citation counts, No, Simple & Extended family size, No, Classification fixed effects, Yes) nocons 
	local replace append
		
		xi:reg `y' firm both other $x $sub i.year NumInventors ClaimCounts,cluster(uspc)
		outreg2 using "`name'.xls", `replace' label keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 

		xi:reg SD`y' firm both other $x $sub i.year NumInventors ClaimCounts,cluster(uspc)
		outreg2 using "`name'.xls", `replace' label keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
	}
di "**********************************"
di "***TABLE 4 p1-p4******************"
di "ADDING YEAR FE and NUM INVENTORS and CLAIM COUNT"
di "**********************************"
local replace replace
forvalues i=1/4 {
	xi:reg SDp`i' firm both other $x $sub i.year NumInventors ClaimCounts,cluster(uspc)
	outreg2 using "RR_SDpca1_4.xls", `replace' label keep(firm both other) nocons 
	local replace append
}

di "**********************************"
di "***TABLE 6 Backward citation FE LASSO****"
di "**********************************"
capture close
log using ../RR_bwfe10_V1.log,replace
use ../lasso10_V2.dta,clear
local replace replace
foreach v in TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep {
	dsregress `v' firm, controls(($x both other i.year NumInventors ClaimCounts) i.sub977b i.sub136b i.sub320b bwcite1b-bwcite6163b)
outreg2 using "RR_bwfe10_V1.xls", `replace' label keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes, Citation fixed effects, Yes) nocons 
local replace append
}

local replace append
foreach v in p1 TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep {
	dsregress SD`v' firm, controls(($x both other i.year NumInventors ClaimCounts) i.sub977b i.sub136b i.sub320b bwcite1b-bwcite6163b)
outreg2 using "RR_bwfe10_V1.xls", `replace' label keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes, Citation fixed effects, Yes) nocons 
}
log close


di "**********************************"
di "***TABLE 5 Attorney FE****"
di "**********************************"
use "tempwide_lawyerid.dta",clear
de,s
forvalues i=1/4927 { /*change # here according to the reshape number of variable*/
capture replace lawyer_rep`i'=0  if lawyer_rep`i'==.
}
merge 1:1 id using "../Lens_cleaned_V6_Domestic_bwcite10rep_V1.dta" 
save "../Lens_cleaned_V6_Domestic_bwcite10rep_V1_lawyerwide"

***********************
***USE REGHDFE********
**********************
log using "../RR_lawyer_v1.log"
*use "../Lens_cleaned_V6_Domestic_bwcite10rep_V1_lawyerwide",clear
keep if _merge==3 /*keep only those with lawyer info */
de,s
*RAW
local replace replace
foreach dv in p1 TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep  {
reghdfe `dv' firm both other $x i.year NumInventors ClaimCounts, absorb(sub136 sub320 sub977 lawyer_rep*) vce(cluster uspc)
outreg2 using "lawyer10_reghdfe.xls", `replace' label keep(firm) nocons 
local replace append
}

*STD
foreach dv in p1 TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep  {
reghdfe SD`dv' firm both other $x i.year NumInventors ClaimCounts, absorb(sub136 sub320 sub977 lawyer_rep*) vce(cluster uspc)
outreg2 using "lawyer10_reghdfe.xls", `replace' label keep(firm) nocons 
local replace append
}

log close


di "************************************************************" 
di "* Hetero* " 
di "************************************************************" 

import excel "topapplicants.xlsx", sheet("topapplicants") firstrow clear

gen Applicant=subinstr(Applicants," ","_",.)
replace Applicant=upper(Applicant)
drop Applicants
save ../RAW/topapplicants.dta,replace
use ../RAW/topapplicants.dta,clear
forvalues i=1/19 {
ren Applicant Applicant`i'
di "Applicant`i'"
save ../RAW/topapplicants`i'
}

use "../Lens_cleaned_V6_Domestic_V1.dta",clear
split Applicants,p(;;) generate(Applicant) /*split multiple applicants from one variable to 19 var*/

forvalues i=1/19 {
di "Applicant`i'"
merge m:1 Applicant`i' using "../topapplicants`i'",generate(_topapplicants`i')
drop if _topapplicants`i'==2  /* not matched in top applicants */
}
gen top100applicants=0
forvalues i=1/19 {
replace top100applicants=1 if _topapplicants`i'==3 /* Matched in top applicants */
drop _topapplicants`i' 
}
tab top100applicants
forvalues i=1/19 {
erase "../topapplicants`i'.dta"
}

save ../OUTDATA/Lens_cleaned_V6_applicants19,replace
use ../OUTDATA/Lens_cleaned_V6_applicants19, clear
cd "../"
*ALL REGRESSION INDV DV
local replace replace
foreach y in SDp1 TRAD_FOG  TRAD_Flesch TRAD_Kincaid $rep{
	qui xi:reg `y' firm both other $x $sub i.year NumInventors ClaimCounts if top100applicants==1,cluster(uspc)
outreg2 using "heter_V2.xls", `replace' label ctitle(Top, "`y'") keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 

local replace append
qui xi:reg `y' firm both other $x $sub i.year NumInventors ClaimCounts if top100applicants==0,cluster(uspc)
outreg2 using "heter_V2.xls", `replace' label  ctitle(Other, "`y'")  keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
}

sum SDp1 if entity==1 & top100applicants==1
sum SDp1 if entity==2 & top100applicants==1
sum SDp1 if entity==1 & top100applicants==0
sum SDp1 if entity==2 & top100applicants==0
sum TRAD_FOG if entity==1 & top100applicants==1
sum TRAD_FOG if entity==2 & top100applicants==1
sum TRAD_FOG if entity==1 & top100applicants==0
sum TRAD_FOG if entity==2 & top100applicants==0

di "**********************************"
di "Table AX Licensing corp"
di "**********************************"

use "../Lens_cleaned_V6_Domestic_V1.dta",clear
cd  "../"
tab LicenseCorp
	local name LicenseCorp_V2
	local replace replace
foreach y in TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep {
	xi:reg `y' LicenseCorp $sub  i.year NumInventors ClaimCounts,cluster(uspc)
	outreg2 using "`name'.xls", `replace' label keep(LicenseCorp) addtext(Citation counts, No, Simple & Extended family size, No, Classification fixed effects, Yes) nocons
	local replace append
}

	*local name LicenseCorp_V2
	*local replace replace
foreach y in p1 TRAD_FOG TRAD_Flesch TRAD_Kincaid  $rep {
	xi:reg SD`y' LicenseCorp $sub  i.year NumInventors ClaimCounts,cluster(uspc)
	outreg2 using "`name'.xls", `replace' label keep(LicenseCorp) addtext(Citation counts, No, Simple & Extended family size, No, Classification fixed effects, Yes) nocons
	local replace append
}



di "**********************************"
di "***TABLE A X Evaluations ****"
di "**********************************"

use "../readability_patentlevel_QualCont_modeLensV6.dta",clear
di "with Student fe "
cd ../OUTPUT
local replace replace
local name SurveyLinguisiticEach_v4
foreach v in Mode_disclosure Mode_readable Mode_MoreDisc Mode_MoreRead Mode_understand Mode_utilize Mode_recreate{
	foreach l in $sowmya {

		di "Control for student FE & patent X"
	xi:areg `v' SD`l' firm both other $x $sub  i.year NumInventors ClaimCounts,absorb(Student) 
	outreg2 using "`name'.xls", `replace' label keep(SD`l') addtext(student FE, Yes, Patent controls, Yes) nocons 
	local replace append
}
}



di "************************************************************" 
di "* Examples * " 
di "************************************************************" 
/*
	import delimited "${PATENT}RAW/example counts_v2 on full para.csv",clear
	gen id=subinstr(lensid,".txt","",.)
	drop lensid
	replace id=subinstr(id,".txt","",.)
	ren numberofexamples examples
	la data "Example counts_v2 on full para, missing is 0 examples"
	save "${PATENT}RAW/examples_v2.dta",replace
*/
use "examples_v2.dta",clear
de,s
merge 1:1 id using "../Lens_cleaned_V6_Domestic_V1.dta",generate(_example)
drop if _example==1
replace examples=0 if _example==2 /*missing to no example*/

save "../Lens_cleaned_V6_examples.dta",replace

use "../Lens_cleaned_V6_examples.dta",clear
/* */
local replace replace
foreach dv in SDp1 TRAD_FOG  TRAD_Flesch TRAD_Kincaid $rep {
reghdfe `dv' examples firm both other $x i.year NumInventors ClaimCounts, absorb(sub136 sub320 sub977) vce(cluster uspc)
outreg2 using "RR_examples_V4.xls", `replace' label keep(examples firm ) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
local replace append
}

local replace replace
foreach dv in SDp1 TRAD_FOG  TRAD_Flesch TRAD_Kincaid $rep {
xi: reg `dv' examples firm both other $x i.year NumInventors ClaimCounts i.sub136 i.sub320 i.sub977, vce(cluster uspc)
outreg2 using "RR_examples_V5.xls", `replace' label keep(examples firm ) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
local replace append
}

di "**********************************************"
di "**********RWOLF*******************************"
di "**********************************************"

di "get adjusted p values"
capture log close
log using "../20220424_rwolf_V2.log",replace

xi:rwolf $sdread, indepvar(firm both other) controls($x $sub i.year NumInventors ClaimCounts) cluster(uspc) reps(250) seed(825)
log close

di "get coefficients"

	local name rwolf_coefficient_V1/*use resolved NPL*/
	local replace replace
foreach y in $sdread {
		
		xi:reg `y' firm both other $x $sub i.year NumInventors ClaimCounts,cluster(uspc)
		outreg2 using "`name'.xls", `replace' label keep(firm both other) addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
	 local replace append
	}
di "**********************************************"
di "*************FIGURE 2 MHT RWOF***************"
di "**********************************************"

use "20220426_indvidualestimatesSDrwolf_Cleaned_V2.dta"

 graph bar (asis) firm0 firm1 , nofill over(dv, lab(nolab) sort(firm_coef) descending) legend(label(1 "Not Significant") label(2 "Significant"))  ytitle("Standardized estimates of firms") b1title("Dependent variables: 64 linguistic indicators") ///
  ylabel(,nogrid) graphregion(color(white)) bgcolor(white)

graph save "Graph" "../20220426_MHT_V2.gph"

graph use "../20220426_MHT_V2.gph"
graph export "../OUTPUT/20220426_MHT_V2.pdf",replace
  log close



di "**********************************************"
di "**********SURVEY *************"
di "**********************************************"

use "../readability_patentlevel_QualCont_modeLensV6.dta",clear
/*separate group by evaluated readability*/
	global VAR3 Mode_disclosure Mode_readable Mode_MoreDisc Mode_understand Mode_utilize Mode_recreate UniPatent 
eststo EvHardToRead:  estpost summarize $VAR3  if Mode_MoreRead == 0
eststo EvEasyToRead:  estpost summarize $VAR3  if Mode_MoreRead == 1
eststo EvDifference: quietly estpost ttest $VAR3 , by(Mode_MoreRead) unequal
esttab EvHardToRead EvEasyToRead EvDifference using "../OUTPUT/byEvaReadQC_V1.csv", ///
cells("mean(pattern(1 1 0) fmt(2)) s


di "student FE"
cd ../OUTPUT
local replace replace
local name SurveyLinguisiticEach_v4
foreach v in Mode_disclosure Mode_readable Mode_MoreDisc Mode_MoreRead Mode_understand Mode_utilize Mode_recreate{
	foreach l in $sowmya {

		di "Control for student FE & patent X"
	xi:areg `v' SD`l' firm both other $x $sub  i.year NumInventors ClaimCounts,absorb(Student) 
	outreg2 using "`name'.xls", `replace' label keep(SD`l') addtext(student FE, Yes, Patent controls, Yes) nocons 
	local replace append
}
}

di "**********************************************"
di "******OFFICE ACTION -112a rejetion***********"
di "**********************************************"

use "../Lens_firstaction112a_v3.dta",clear

di "Note: specify _FirstAction112==3 to compare only matched sample with first rejection"
do  "0_global_v1.do"


cd ../OUTPUT
	local name first112a_V1
	local replace replace
foreach v in  FirstRejection112a {
foreach l in $sowmya {		
		xi:reg `v' SD`l' firm both other $x $sub  i.year NumInventors ClaimCounts,cluster(uspc)
		outreg2 using "`name'.xls", `replace' label keep(SD`l') addtext(Citation counts, Yes, Simple & Extended family size, Yes, Classification fixed effects, Yes) nocons 
		local replace append
	}
	}	
	

